<template>
  <div class="holiday-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span class="title">节假日管理</span>
          <div class="right-button">
            <el-button type="primary" icon="Plus" @click="handleAdd"> 新增 </el-button>
            <el-button icon="Download" @click="handleImport"> 导入 </el-button>
            <el-button icon="Upload" @click="handleExport"> 导出 </el-button>
          </div>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="节假日名称" prop="holidayName">
          <el-input
            v-model="queryParams.holidayName"
            placeholder="请输入节假日名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="假日类型" prop="holidayType">
          <el-select
            v-model="queryParams.holidayType"
            placeholder="假日类型"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="dict in holidayTypes"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="日期范围" prop="dateRange">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <el-table v-loading="loading" :data="holidayList" style="width: 100%" border>
        <el-table-column prop="holidayId" label="节假日编号" align="center" width="100" />
        <el-table-column prop="holidayName" label="节假日名称" align="center" width="150" />
        <el-table-column prop="holidayType" label="假日类型" align="center" width="120">
          <template #default="scope">
            <el-tag :type="getHolidayTypeType(scope.row.holidayType)">{{
              getHolidayTypeLabel(scope.row.holidayType)
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="year" label="年份" align="center" width="80" />
        <el-table-column prop="startDate" label="开始日期" align="center" width="120" />
        <el-table-column prop="endDate" label="结束日期" align="center" width="120" />
        <el-table-column prop="days" label="天数" align="center" width="80" />
        <el-table-column
          prop="description"
          label="描述"
          align="center"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column prop="status" label="状态" align="center" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">{{
              scope.row.status === 1 ? "启用" : "禁用"
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template #default="scope">
            <el-button size="small" type="primary" link @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              :type="getButtonType(scope.row.status)"
              link
              @click="handleChangeStatus(scope.row)"
              >{{ scope.row.status === 1 ? "禁用" : "启用" }}</el-button
            >
            <el-button size="small" type="danger" link @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

// 定义节假日类型选项
const holidayTypes = [
  { value: 1, label: "法定节假日", type: "danger" },
  { value: 2, label: "调休工作日", type: "warning" },
  { value: 3, label: "周末", type: "info" },
  { value: 4, label: "特殊假日", type: "success" }
];

// 获取节假日类型标签
const getHolidayTypeLabel = type => {
  const item = holidayTypes.find(item => item.value === type);
  return item ? item.label : "未知";
};

// 获取节假日类型样式
const getHolidayTypeType = type => {
  const item = holidayTypes.find(item => item.value === type);
  return item ? (item.type as "" | "warning" | "success" | "danger" | "info") : "";
};

// 加载状态
const loading = ref(false);

// 节假日列表数据
const holidayList = ref([]);

// 总记录数
const total = ref(0);

// 日期范围
const dateRange = ref<any>([]);

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  holidayName: "",
  holidayType: null,
  startDate: "",
  endDate: ""
});

// 获取列表数据
const getList = () => {
  loading.value = true;
  // 这里应该是实际的API调用，目前使用模拟数据
  setTimeout(() => {
    holidayList.value = [
      {
        holidayId: 1,
        holidayName: "国庆节",
        holidayType: 1,
        year: 2023,
        startDate: "2023-10-01",
        endDate: "2023-10-07",
        days: 7,
        description: "国庆节放假7天",
        status: 1
      },
      {
        holidayId: 2,
        holidayName: "国庆节补班",
        holidayType: 2,
        year: 2023,
        startDate: "2023-09-30",
        endDate: "2023-09-30",
        days: 1,
        description: "国庆节前补班",
        status: 1
      }
    ];
    total.value = 2;
    loading.value = false;
  }, 500);
};

// 查询操作
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

// 重置操作
const resetQuery = () => {
  dateRange.value = [];
  queryParams.holidayName = "";
  queryParams.holidayType = null;
  queryParams.startDate = "";
  queryParams.endDate = "";
  handleQuery();
};

// 新增操作
const handleAdd = () => {
  ElMessage.info("点击了新增按钮");
};

// 编辑操作
const handleEdit = row => {
  ElMessage.info(`编辑节假日记录：${row.holidayName}`);
};

// 改变状态
const handleChangeStatus = row => {
  const statusText = row.status === 1 ? "禁用" : "启用";
  ElMessageBox.confirm(`确认${statusText}该节假日吗？`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      ElMessage.success(`${statusText}成功`);
    })
    .catch(() => {});
};

// 删除操作
const handleDelete = row => {
  ElMessageBox.confirm(`确认删除节假日记录：${row.holidayName}吗？`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};

// 导入操作
const handleImport = () => {
  ElMessage.info("点击了导入按钮");
};

// 导出操作
const handleExport = () => {
  ElMessage.info("点击了导出按钮");
};

// 分页大小变化事件处理
const handleSizeChange = val => {
  queryParams.pageSize = val;
  getList();
};

// 分页页码变化事件处理
const handleCurrentChange = val => {
  queryParams.pageNum = val;
  getList();
};

// 添加状态类型获取方法
const getStatusType = (status: number): "" | "warning" | "success" | "danger" | "info" => {
  return status === 1 ? "success" : "danger";
};

// 添加按钮类型获取方法
const getButtonType = (
  status: number
): "default" | "primary" | "success" | "warning" | "danger" | "info" | "text" => {
  return status === 1 ? "danger" : "success";
};

// 页面加载时获取列表数据
onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.holiday-container {
  padding: 0px;

  .card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .title {
      font-size: 16px;
      font-weight: bold;
    }

    .right-button {
      display: flex;
      gap: 10px;
    }
  }
}
</style>
